<template>
  <el-dialog v-el-drag-dialog :title="title" width="60%" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose" destroy-on-close>
    <div style="height: calc(85vh - 135px)">
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item prop="Q_processName_S_LK">
            <el-input v-model="queryData.Q_processName_S_LK" placeholder="流程名称" />
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item m-l-10" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="process_table"
          v-loading="tableLoading"
          row-key="definitionId"
          :data="tableData"
          height="320px"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
        >
          <el-table-column v-if="!single" type="selection" width="32" />&nbsp;
          <el-table-column v-if="single" width="32">
            <template slot-scope="scope">
              <el-radio
                v-model="selectRadio"
                :label="scope.row.userId"
              >&nbsp;
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column align="center" label="流程标识" prop="processKey" show-overflow-tooltip />
          <el-table-column align="center" label="流程名称" prop="processName" show-overflow-tooltip />
          <el-table-column align="center" label="流程分类">
            <template slot-scope="scope">
              <el-tag :type="getDictType('category', scope.row.category)" v-text="getDictValue('category', scope.row.category)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="流程版本" width="90">
            <template slot-scope="scope">
              <el-tag size="medium" >v{{ scope.row.version }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="流程状态" width="90">
            <template slot-scope="scope">
              <el-tag type="success" v-if="!scope.row.suspended">激活</el-tag>
              <el-tag type="warning" v-if="scope.row.suspended">挂起</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="80" fixed="right">
            <template slot-scope="scope">
              <el-link type="success" icon="el-icon-video-play" @click="handleStartProcess(scope.row)">申请</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData(false)" />
      </el-card>
    </div>
  </el-dialog>
</template>

<script>
import { categoryGroup, definitionTable } from '@/api/tool/workflow'

export default {
  name: 'DefinitionTableDialog',
  props: {
    title: {
      type: String,
      default: '流程列表'
    },
    appendToBody: {
      type: Boolean,
      default: true
    },
    single: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      showSearch: true,
      queryData: {
        Q_processName_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      optionOfCategory: []
    }
  },
  methods: {
    init() {
      this.visible = true
      this.getDictData()
      this.getTableData()
    },
    getDictData() {
      categoryGroup().then(response => {
        this.optionOfCategory = response
      }).catch(e => {})
    },
    getDictType(property, value) {
      return this.$api.getDictElement(this.optionOfCategory, value)[0]
    },
    getDictValue(property, value) {
      return this.$api.getDictElement(this.optionOfCategory, value)[1]
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.tableQuery.page = 1
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      definitionTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.process_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleStartProcess(row) {
      this.$emit('startTask', row.deploymentId, row.definitionId, row.processName)
      this.onClose()
    },
    onClose() {
      this.$refs.queryForm.resetFields()
      this.visible = false
    }
  }
}
</script>
